The Nature of Homoiconicity
Homoiconic language: A language in which the internal representation is expressed in the language itself. In Elixir, source code is not just text; it is a nested data structure known as the Abstract Syntax Tree (AST).
1. Logic Rules of Representation
Elixir simplifies the AST by representing basic literals as themselves. Atoms, numbers, lists (including keyword lists), binaries, and tuples with two elements are represented internally without complex wrapping.
[do: 1]
iex> quote do: "binaries"
"binaries"
2. The AST Tuple Structure
Most code transformations result in a three-element tuple: {function_name, metadata, arguments}. For instance, a function call like 1 + 2 becomes {:+, [line: 1], [1, 2]}. This allows the language to treat Code as Data.
3. Macros: Delaying Execution
Macros are "a way of delaying the execution of clauses by injecting code back into the internal representation of our program." While standard functions evaluate arguments before calling, macros receive the raw AST, allowing for logic injection before compilation.
$$\text{Source Code} \xrightarrow{\text{Quote}} \text{AST Tuple} \xrightarrow{\text{Macro}} \text{Transformed AST}$$